package blog.service.impl;


import blog.dao.ChapterSpecialDao;
import blog.pojo.ChapterSpecial;
import blog.service.ChapterSpecialService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ChapterSpecialServiceImpl implements ChapterSpecialService {

    @Autowired
    private ChapterSpecialDao chapterSpecialDao;

    @Override
    public Page<ChapterSpecial> getPage(Page page, ChapterSpecial chapterSpecial, String keywords) throws Exception {
        if (page == null) return null;
        QueryWrapper<ChapterSpecial> queryWrapper = new QueryWrapper<>(chapterSpecial)
                .ne("isuse", 2);
        if (keywords != null) {
            queryWrapper.and(Wrapper -> Wrapper.like("name", keywords).or().like("title", keywords));
        }
        return chapterSpecialDao.selectPage(page, queryWrapper);
    }

    @Override
    public ChapterSpecial getInfo(Integer id) throws Exception {
        return chapterSpecialDao.selectById(id);
    }

    @Override
    public Boolean update(ChapterSpecial chapterSpecial) throws Exception {
        return chapterSpecialDao.updateById(chapterSpecial) > 0;
    }

    @Override
    public Boolean insert(ChapterSpecial chapterSpecial) throws Exception {
        return chapterSpecialDao.insert(chapterSpecial) > 0;
    }

    @Override
    public ChapterSpecial selectByName(String name) throws Exception {
        return chapterSpecialDao.selectOne(new QueryWrapper<>(new ChapterSpecial().setName(name).setIsuse(1)));
    }
}
